<template>
  <a-card :bordered="false">
    <!-- <div class="card-title">{{ $route.meta.title }} {{ bookName }}</div> -->
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="试卷名称">
            <a-input v-decorator="['name']" placeholder="请输入试卷名称" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="plus" @click="addOrEdit({},1)">添加</a-button>
          </a-form-item>
        </a-form>
      </a-row>
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        
      </div>
    </div>
    <s-table ref="table" rowKey="id" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
      <span slot="url" slot-scope="text, item">
        <a-button class="fl-l" type="primary" icon="copy" @click="handleCopy(text)">点击复制</a-button>
      </span>
      <span slot="qrcode" slot-scope="text, item">
        <qrcodeImg :content="item.url" />
      </span>
      <span slot="status" slot-scope="text, item">
        <a-tag class="cur-p" :color="text === 10 ? 'green' : 'red'" @click="handleUpdateStatus(item.id, text)">{{
          text === 10 ? '正常' : '停用'
        }}</a-tag>
      </span>
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="text, item">
        <a v-action:edit @click="addOrEdit(item,2)">编辑</a>
        <a style="color: red" v-action:delete @click="handleDelete(item.id)">删除</a>
      </span>
    </s-table>
    <a-modal
    :width="620"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="btnSave"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 13 }">
        <a-form-item label="试卷名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入试卷名称' }] }]"/>
        </a-form-item>
        <a-form-item label="说明">
          <a-textarea v-decorator="['desc']" :autoSize="{ minRows: 4, maxRows: 8 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>    
    <BatchAddNullPage ref="nullPage" @handleSubmit="handleRefresh(false)" />
    <makeCode ref="makeCode" />
    <sync ref="syncForm" @handleSubmit="handleRefresh(false)" />
  </a-card> 
</template>

<script>
import * as Api from '@/api/book/page'
import { STable } from '@/components'
import BatchAddNullPage from '@/views/book/modules/BatchAddNullPage'
import qrcodeImg from '@/views/book/modules/qrcodeImg'
import makeCode from '@/views/book/modules/makeCode'
import WorkTypeEnum from '@/common/enum/workType'
import sync from '@/views/book/modules/sync'

export default {
  // name: 'Index',
  components: {
    STable,
    qrcodeImg,
    makeCode,
    sync,
    BatchAddNullPage
  },
  data() {
    return {
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '页面Url',
          dataIndex: 'url',
          scopedSlots: { customRender: 'url' }
        },
        {
          title: '二维码',
          dataIndex: 'qrcode',
          scopedSlots: { customRender: 'qrcode' }
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      visible:false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.questionsList({ ...param, ...this.queryParam }).then((response) => response.data.list)
      }
    }
  },
  created() {
  },
  methods: {
    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    // 修改状态()
    handleUpdateStatus (id, state) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要' + (state === 10 ? '停用' : '启用') + '该记录吗?',
        onOk () {
          return Api.questionsChangeStatus({ id })
            .then((result) => {
              // 显示成功
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => modal.destroy())
        }
      })
    },
    // 确认搜索
    handleSearch(e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },
    handleCopy(text) {
      window.getSelection().removeAllRanges()
      const textarea = document.createElement('textarea')
      textarea.value = text
      document.body.appendChild(textarea)
      textarea.select()
      document.execCommand('copy')
      textarea.remove()
      this.$message.success('复制成功', 1.5)
    },
    addOrEdit (item, num) { // 1添加 2编辑
      this.modalType=num
      this.visible = true
      this.$nextTick(() => {
        if (num === 2) {
          this.editId=item.id
          this.form.setFieldsValue({
            name: item.name,
            desc: item.desc
          })
        } else {
          this.form.resetFields()
        }
      })
    },
    handleCancel (item) {
      this.visible = false
    },
    btnSave (e) { // 保存
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          const handle = this.modalType === 1 ? Api.addQuestions : Api.questionsEdit
          console.log({name: values.name, desc: values.desc || '', id: this.modalType === 2 ? this.editId : undefined})
          handle({name: values.name, desc: values.desc || '', id: this.modalType === 2 ? this.editId : undefined}).then((res) => {
            this.$message.success('操作成功', 1.5)
            this.handleRefresh(true)
            this.visible = false
            this.confirmLoading = false
          }).finally(() => {
            this.confirmLoading = false
          })
        } else {
          // 如果有错误，停止加载状态
          this.confirmLoading = false
        }
      })
    },
    handleDelete (id) { // 删除
      const app = this
      const modal = this.$confirm({
        title: '提示',
        content: '确定删除',
        onOk () {
          return Api.questionsDelete({ id })
            .then((result) => {
              app.$message.success('操作成功', 1.5)
              app.handleRefresh(true)
            })
            .finally((result) => modal.destroy())
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
